home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / core.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-16  |  11.6 KB  |  477 lines

  1. /* Work with core dump and executable files, for GDB.
  2.    Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include <fcntl.h>
  24. #include "frame.h"  /* required by inferior.h */
  25. #include "inferior.h"
  26. #include "symtab.h"
  27. #include "command.h"
  28. #include "bfd.h"
  29. #include "target.h"
  30. #include "gdbcore.h"
  31.  
  32. CORE_ADDR data_start;
  33. CORE_ADDR data_end;
  34. CORE_ADDR stack_start;
  35. CORE_ADDR stack_end;
  36. CORE_ADDR text_start;
  37. CORE_ADDR text_end;
  38. CORE_ADDR exec_data_start;
  39. CORE_ADDR exec_data_end;
  40.  
  41. #ifdef SOLIB_ADD
  42. static int 
  43. solib_add_stub PARAMS ((char *));
  44. #endif
  45.  
  46. static void
  47. core_close PARAMS ((int));
  48.  
  49. static void
  50. core_open PARAMS ((char *, int));
  51.  
  52. static void
  53. core_detach PARAMS ((char *, int));
  54.  
  55. static void
  56. get_core_registers PARAMS ((int));
  57.  
  58. static void
  59. core_files_info PARAMS ((struct target_ops *));
  60.  
  61. extern int sys_nerr;
  62. extern char *sys_errlist[];
  63. extern char *sys_siglist[];
  64.  
  65. extern char registers[];
  66.  
  67. /* Hook for `exec_file_command' command to call.  */
  68.  
  69. void (*exec_file_display_hook) PARAMS ((char *)) = NULL;
  70.  
  71. /* Binary file diddling handle for the core file.  */
  72.  
  73. bfd *core_bfd = NULL;
  74.  
  75. /* Forward decl */
  76. extern struct target_ops core_ops;
  77.  
  78.  
  79. /* Discard all vestiges of any previous core file
  80.    and mark data and stack spaces as empty.  */
  81.  
  82. /* ARGSUSED */
  83. static void
  84. core_close (quitting)
  85.      int quitting;
  86. {
  87.   if (core_bfd) {
  88.     free (bfd_get_filename (core_bfd));
  89.     bfd_close (core_bfd);
  90.     core_bfd = NULL;
  91. #ifdef CLEAR_SOLIB
  92.     CLEAR_SOLIB ();
  93. #endif
  94.     if (core_ops.to_sections) {
  95.       free ((PTR)core_ops.to_sections);
  96.       core_ops.to_sections = NULL;
  97.       core_ops.to_sections_end = NULL;
  98.     }
  99.   }
  100. }
  101.  
  102. #ifdef SOLIB_ADD
  103. /* Stub function for catch_errors around shared library hacking. */
  104.  
  105. static int 
  106. solib_add_stub (from_tty)
  107.      char *from_tty;
  108. {
  109.     SOLIB_ADD (NULL, (int)from_tty, &core_ops);
  110.     return 0;
  111. }
  112. #endif /* SOLIB_ADD */
  113.  
  114. /* This routine opens and sets up the core file bfd */
  115.  
  116. static void
  117. core_open (filename, from_tty)
  118.      char *filename;
  119.      int from_tty;
  120. {
  121.   const char *p;
  122.   int siggy;
  123.   struct cleanup *old_chain;
  124.   char *temp;
  125.   bfd *temp_bfd;
  126.   int ontop;
  127.   int scratch_chan;
  128.  
  129.   target_preopen (from_tty);
  130.   if (!filename)
  131.     {
  132.       error (core_bfd? 
  133.        "No core file specified.  (Use `detach' to stop debugging a core file.)"
  134.      : "No core file specified.");
  135.     }
  136.  
  137.   filename = tilde_expand (filename);
  138.   if (filename[0] != '/') {
  139.     temp = concat (current_directory, "/", filename, NULL);
  140.     free (filename);
  141.     filename = temp;
  142.   }
  143.  
  144.   old_chain = make_cleanup (free, filename);
  145.  
  146.   scratch_chan = open (filename, write_files? O_RDWR: O_RDONLY, 0);
  147.   if (scratch_chan < 0)
  148.     perror_with_name (filename);
  149.  
  150.   temp_bfd = bfd_fdopenr (filename, NULL, scratch_chan);
  151.   if (temp_bfd == NULL)
  152.     {
  153.       perror_with_name (filename);
  154.     }
  155.  
  156.   if (!bfd_check_format (temp_bfd, bfd_core))
  157.     {
  158.       /* Do it after the err msg */
  159.       make_cleanup (bfd_close, temp_bfd);
  160.       error ("\"%s\" is not a core dump: %s", filename, bfd_errmsg(bfd_error));
  161.     }
  162.  
  163.   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
  164.  
  165.   discard_cleanups (old_chain);        /* Don't free filename any more */
  166.   unpush_target (&core_ops);
  167.   core_bfd = temp_bfd;
  168.   old_chain = make_cleanup (core_close, core_bfd);
  169.  
  170.   validate_files ();
  171.  
  172.   /* Find the data section */
  173.   if (build_section_table (core_bfd, &core_ops.to_sections,
  174.                &core_ops.to_sections_end))
  175.     error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
  176.        bfd_errmsg (bfd_error));
  177.  
  178.   ontop = !push_target (&core_ops);
  179.   discard_cleanups (old_chain);
  180.  
  181.   p = bfd_core_file_failing_command (core_bfd);
  182.   if (p)
  183.     printf ("Core was generated by `%s'.\n", p);
  184.  
  185.   siggy = bfd_core_file_failing_signal (core_bfd);
  186.   if (siggy > 0)
  187.     printf ("Program terminated with signal %d, %s.\n", siggy,
  188.         siggy < NSIG ? sys_siglist[siggy] : "(undocumented)");
  189.  
  190.   if (ontop) {
  191.     /* Fetch all registers from core file */
  192.     target_fetch_registers (-1);
  193.  
  194.     /* Add symbols and section mappings for any shared libraries */
  195. #ifdef SOLIB_ADD
  196.     (void) catch_errors (solib_add_stub, (char *)from_tty, (char *)0);
  197. #endif
  198.  
  199.     /* Now, set up the frame cache, and print the top of stack */
  200.     set_current_frame (create_new_frame (read_register (FP_REGNUM),
  201.                      read_pc ()));
  202.     select_frame (get_current_frame (), 0);
  203.     print_stack_frame (selected_frame, selected_frame_level, 1);
  204.   } else {
  205.     printf (
  206. "Warning: you won't be able to access this core file until you terminate\n\
  207. your %s; do ``info files''\n", current_target->to_longname);
  208.   }
  209. }
  210.  
  211. static void
  212. core_detach (args, from_tty)
  213.      char *args;
  214.      int from_tty;
  215. {
  216.   if (args)
  217.     error ("Too many arguments");
  218.   unpush_target (&core_ops);
  219.   if (from_tty)
  220.     printf ("No core file now.\n");
  221. }
  222.  
  223. /* Backward compatability with old way of specifying core files.  */
  224.  
  225. void
  226. core_file_command (filename, from_tty)
  227.      char *filename;
  228.      int from_tty;
  229. {
  230.   dont_repeat ();            /* Either way, seems bogus. */
  231.   if (!filename)
  232.     core_detach (filename, from_tty);
  233.   else
  234.     core_open (filename, from_tty);
  235. }
  236.  
  237.  
  238. /* Call this to specify the hook for exec_file_command to call back.
  239.    This is called from the x-window display code.  */
  240.  
  241. void
  242. specify_exec_file_hook (hook)
  243.      void (*hook) PARAMS ((char *));
  244. {
  245.   exec_file_display_hook = hook;
  246. }
  247.  
  248. /* The exec file must be closed before running an inferior.
  249.    If it is needed again after the inferior dies, it must
  250.    be reopened.  */
  251.  
  252. void
  253. close_exec_file ()
  254. {
  255. #ifdef FIXME
  256.   if (exec_bfd)
  257.     bfd_tempclose (exec_bfd);
  258. #endif
  259. }
  260.  
  261. void
  262. reopen_exec_file ()
  263. {
  264. #ifdef FIXME
  265.   if (exec_bfd)
  266.     bfd_reopen (exec_bfd);
  267. #endif
  268. }
  269.  
  270. /* If we have both a core file and an exec file,
  271.    print a warning if they don't go together.  */
  272.  
  273. void
  274. validate_files ()
  275. {
  276.   if (exec_bfd && core_bfd)
  277.     {
  278.       if (!core_file_matches_executable_p (core_bfd, exec_bfd))
  279.     printf ("Warning: core file may not match specified executable file.\n");
  280.       else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
  281.     printf ("Warning: exec file is newer than core file.\n");
  282.     }
  283. }
  284.  
  285. /* Return the name of the executable file as a string.
  286.    ERR nonzero means get error if there is none specified;
  287.    otherwise return 0 in that case.  */
  288.  
  289. char *
  290. get_exec_file (err)
  291.      int err;
  292. {
  293.   if (exec_bfd) return bfd_get_filename(exec_bfd);
  294.   if (!err)     return NULL;
  295.  
  296.   error ("No executable file specified.\n\
  297. Use the \"file\" or \"exec-file\" command.");
  298.   return NULL;
  299. }
  300.  
  301. static void
  302. core_files_info (t)
  303.   struct target_ops *t;
  304. {
  305.   print_section_info (t, core_bfd);
  306. }
  307.  
  308. /* Report a memory error with error().  */
  309.  
  310. void
  311. memory_error (status, memaddr)
  312.      int status;
  313.      CORE_ADDR memaddr;
  314. {
  315.  
  316.   if (status == EIO)
  317.     {
  318.       /* Actually, address between memaddr and memaddr + len
  319.      was out of bounds. */
  320.       error ("Cannot access memory at address %s.", local_hex_string(memaddr));
  321.     }
  322.   else
  323.     {
  324.       if (status >= sys_nerr || status < 0)
  325.     error ("Error accessing memory address %s: unknown error (%d).",
  326.            local_hex_string(memaddr), status);
  327.       else
  328.     error ("Error accessing memory address %s: %s.",
  329.            local_hex_string(memaddr), sys_errlist[status]);
  330.     }
  331. }
  332.  
  333. /* Same as target_read_memory, but report an error if can't read.  */
  334. void
  335. read_memory (memaddr, myaddr, len)
  336.      CORE_ADDR memaddr;
  337.      char *myaddr;
  338.      int len;
  339. {
  340.   int status;
  341.   status = target_read_memory (memaddr, myaddr, len);
  342.   if (status != 0)
  343.     memory_error (status, memaddr);
  344. }
  345.  
  346. /* Same as target_write_memory, but report an error if can't write.  */
  347. void
  348. write_memory (memaddr, myaddr, len)
  349.      CORE_ADDR memaddr;
  350.      char *myaddr;
  351.      int len;
  352. {
  353.   int status;
  354.  
  355.   status = target_write_memory (memaddr, myaddr, len);
  356.   if (status != 0)
  357.     memory_error (status, memaddr);
  358. }
  359.  
  360. /* Read an integer from debugged memory, given address and number of bytes.  */
  361.  
  362. long
  363. read_memory_integer (memaddr, len)
  364.      CORE_ADDR memaddr;
  365.      int len;
  366. {
  367.   char cbuf;
  368.   short sbuf;
  369.   int ibuf;
  370.   long lbuf;
  371.  
  372.   if (len == sizeof (char))
  373.     {
  374.       read_memory (memaddr, &cbuf, len);
  375.       return cbuf;
  376.     }
  377.   if (len == sizeof (short))
  378.     {
  379.       read_memory (memaddr, (char *)&sbuf, len);
  380.       SWAP_TARGET_AND_HOST (&sbuf, sizeof (short));
  381.       return sbuf;
  382.     }
  383.   if (len == sizeof (int))
  384.     {
  385.       read_memory (memaddr, (char *)&ibuf, len);
  386.       SWAP_TARGET_AND_HOST (&ibuf, sizeof (int));
  387.       return ibuf;
  388.     }
  389.   if (len == sizeof (lbuf))
  390.     {
  391.       read_memory (memaddr, (char *)&lbuf, len);
  392.       SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf));
  393.       return lbuf;
  394.     }
  395.   error ("Cannot handle integers of %d bytes.", len);
  396.   return -1;    /* for lint */
  397. }
  398.  
  399. /* Get the registers out of a core file.  This is the machine-
  400.    independent part.  Fetch_core_registers is the machine-dependent
  401.    part, typically implemented in the xm-file for each architecture.  */
  402.  
  403. /* We just get all the registers, so we don't use regno.  */
  404. /* ARGSUSED */
  405. static void
  406. get_core_registers (regno)
  407.      int regno;
  408. {
  409.   sec_ptr reg_sec;
  410.   unsigned size;
  411.   char *the_regs;
  412.  
  413.   reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
  414.   if (!reg_sec) goto cant;
  415.   size = bfd_section_size (core_bfd, reg_sec);
  416.   the_regs = alloca (size);
  417.   if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0, size))
  418.     {
  419.       fetch_core_registers (the_regs, size, 0,
  420.                 (unsigned) bfd_section_vma (abfd,reg_sec));
  421.     }
  422.   else
  423.     {
  424. cant:
  425.       fprintf (stderr, "Couldn't fetch registers from core file: %s\n",
  426.            bfd_errmsg (bfd_error));
  427.     }
  428.  
  429.   /* Now do it again for the float registers, if they exist.  */
  430.   reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
  431.   if (reg_sec) {
  432.     size = bfd_section_size (core_bfd, reg_sec);
  433.     the_regs = alloca (size);
  434.     if (bfd_get_section_contents (core_bfd, reg_sec, the_regs, (file_ptr)0,
  435.                   size))
  436.       {
  437.     fetch_core_registers (the_regs, size, 2,
  438.                   (unsigned) bfd_section_vma (abfd,reg_sec));
  439.       }
  440.     else
  441.       {
  442.     fprintf (stderr, "Couldn't fetch register set 2 from core file: %s\n",
  443.          bfd_errmsg (bfd_error));
  444.       }
  445.   }
  446.   registers_fetched();
  447. }
  448.  
  449. struct target_ops core_ops = {
  450.     "core", "Local core dump file",
  451.     "Use a core file as a target.  Specify the filename of the core file.",
  452.     core_open, core_close,
  453.     child_attach, core_detach, 0, 0, /* resume, wait */
  454.     get_core_registers, 
  455.     0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
  456.     xfer_memory, core_files_info,
  457.     0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
  458.     0, 0, 0, 0, 0, /* terminal stuff */
  459.     0, 0, 0, /* kill, load, lookup sym */
  460.     child_create_inferior, 0, /* mourn_inferior */
  461.     core_stratum, 0, /* next */
  462.     0, 1, 1, 1, 0,    /* all mem, mem, stack, regs, exec */
  463.     0, 0,            /* section pointers */
  464.     OPS_MAGIC,        /* Always the last thing */
  465. };
  466.  
  467. void
  468. _initialize_core()
  469. {
  470.  
  471.   add_com ("core-file", class_files, core_file_command,
  472.        "Use FILE as core dump for examining memory and registers.\n\
  473. No arg means have no core file.  This command has been superseded by the\n\
  474. `target core' and `detach' commands.");
  475.   add_target (&core_ops);
  476. }
  477.